package org.kkhweb.checkbook;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class SimpleJSONParser {

	private JSONParser json;
	private JSONArray arr;
        private JSONObject obj;
        private boolean array;
	private String _errorMsg;
	private boolean _hasError;

	public SimpleJSONParser() {
		this.json = new JSONParser();
		this._hasError = false;
		this._errorMsg = "";
	}

	public void parse(String jsonText) {
		try {
                    if(jsonText.charAt(0) == '[') {
			this.arr = (JSONArray) this.json.parse(jsonText);
                        this.array = true;
			this._setErrorStatus(false);
                    } else {
			this.obj = (JSONObject) this.json.parse(jsonText);
                        this.array = false;
			this._setErrorStatus(false);
                    }
		} catch (ParseException e) {
			this._setErrorStatus(true);
			this._setErrorMsg(e.getMessage());
		}
	}

	public JSONObject fetch() {
		return (JSONObject) this.arr.get(1);
	}

	public JSONObject fetch(int key) {
		return (JSONObject) this.arr.get(key);
	}

	public String getStr(String key)
	{
            if(!this.array) {
                return (String) this.obj.get(key);
            } else {
                JSONObject tmp = (JSONObject) this.arr.get(1);
		return (String) tmp.get(key);
            }
	}

	public String getStr(int num, String key)
	{
             if(!this.array) {
                return (String) this.obj.get(key);
             } else {
		JSONObject tmp = (JSONObject) this.arr.get(num);
		return (String) tmp.get(key);
             }
	}

	public int getInt(String key)
	{
            if(!this.array) {
                return Integer.parseInt((String) this.obj.get(key));
            } else {
		JSONObject tmp = (JSONObject) this.arr.get(1);
		return Integer.parseInt((String) tmp.get(key));
            }
	}

	public int getInt(int num, String key)
	{
            if(!this.array) {
                return Integer.parseInt((String) this.obj.get(key));
            } else {
		JSONObject tmp = (JSONObject) this.arr.get(num);
		return Integer.parseInt((String) tmp.get(key));
            }
	}

	public boolean getBool(String key)
	{
            if(!this.array) {
                return Boolean.parseBoolean((String) this.obj.get(key));
            } else {
		JSONObject tmp = (JSONObject) this.arr.get(1);
		return Boolean.parseBoolean((String) tmp.get(key));
            }
	}

	public boolean getBool(int num, String key)
	{
            if(!this.array) {
                return Boolean.parseBoolean((String) this.obj.get(key));
            } else {
		JSONObject tmp = (JSONObject) this.arr.get(num);
		return Boolean.parseBoolean((String) tmp.get(key));
            }
	}

	public int size() {
            if(!this.array) {
                return 1;
            } else {
		return this.arr.size();
            }
	}

	public boolean hasError() {
		return this._hasError;
	}

	public String getError() {
		return this._errorMsg;
	}

	// Private Methods
	private void _setErrorMsg(String msg) {
		this._errorMsg = msg;
	}

	private void _setErrorStatus(boolean status) {
		this._hasError = status;
	}

}